package com.atguigu.guli.service.ucenter.service.impl;
import java.util.Date;

import com.atguigu.guli.common.base.result.ResultCodeEnum;
import com.atguigu.guli.common.base.util.FormUtils;
import com.atguigu.guli.common.base.util.JwtInfo;
import com.atguigu.guli.common.base.util.JwtUtils;
import com.atguigu.guli.common.base.util.MD5;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.exception.MyException;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.vo.LoginVo;
import com.atguigu.guli.service.ucenter.entity.vo.RegisterVo;
import com.atguigu.guli.service.ucenter.mapper.MemberMapper;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author zero
 * @since 2020-05-28
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberService memberService;

    /**
     * 用户注册
     *
     * @param registerVo 注册信息封装
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();

        // 校验参数
        if(StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code)){
            throw new MyException(ResultCodeEnum.PARAM_ERROR);
        }

        // 检验验证码
        String checkCode = (String) redisTemplate.opsForValue().get(mobile);
        if(!code.equals(checkCode)){
            throw new MyException(ResultCodeEnum.CODE_ERROR);
        }

        // 检查用户是否被注册
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(memberQueryWrapper);
        if(count > 0){
            throw new MyException(ResultCodeEnum.REGISTER_MOBLE_ERROR);
        }
        // 注册
        Member member = new Member();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setAvatar("");
        member.setDisabled(false);
        baseMapper.insert(member);
    }

    /**
     * 会员登录
     *
     * @param loginVo 登录信息
     * @return 登录 token
     */
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        // 参数校验
        if(StringUtils.isEmpty(mobile) || !FormUtils.isMobile(mobile) || StringUtils.isEmpty(password)){
            throw new MyException(ResultCodeEnum.PARAM_ERROR);
        }

        // 校验手机号
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile", mobile);
        Member member = baseMapper.selectOne(memberQueryWrapper);
        if (member == null) {
            throw new MyException(ResultCodeEnum.LOGIN_PHONE_ERROR);
        }

        // 检验密码
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new MyException(ResultCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        // 校验用户是否可用
        if(member.getDisabled()){
            throw new MyException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }

        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(member.getId());
        jwtInfo.setNickname(member.getNickname());
        jwtInfo.setAvatar(member.getAvatar());

        return JwtUtils.getJwtToken(jwtInfo, 1800);
    }

    /**
     * 根据openId查询用户信息
     *
     * @param openid openid
     * @return 查询结果
     */
    @Override
    public Member getByOpenid(String openid) {

        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("openid", openid);
        return baseMapper.selectOne(memberQueryWrapper);
    }

    /**
     * 根据会员id查询封装信息
     *
     * @param memberId 会员id
     * @return 查询结果
     */
    @Override
    public MemberDto getMemberDtoByMemberId(String memberId) {
        Member member = baseMapper.selectById(memberId);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member, memberDto);
        return memberDto;
    }


}
